//
//  LLShoppingCarCell.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/9.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLShoppingCarCell.h"
#import "LLShoppingCartModel.h"

@interface LLShoppingCarCell ()
@property (strong, nonatomic) LLButton *selecteBtn;
@property (strong, nonatomic) UIImageView *imgView;
@property (strong, nonatomic) UILabel *titleLbl;
@property (strong, nonatomic) UILabel *priceLbl;
@property (strong, nonatomic) LLButton *reduceBtn;
@property (strong, nonatomic) LLButton *addBtn;
//@property (strong, nonatomic) UILabel *countLbl;
@property (strong, nonatomic) UITextField *countF;
@end

@implementation LLShoppingCarCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    UIView *bgView = UIView.new;
    bgView.frame = CGRectMake(10, 0, LLScreenWidth-20, 110);
    bgView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:bgView];
    
    _selecteBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"unselected" titleColor:nil font:0];
    _selecteBtn.frame = CGRectMake(0, (110-40)/2.0, 40, 40);
    [_selecteBtn setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
    [_selecteBtn addTarget:self action:@selector(selecteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_selecteBtn];
    
//    CGFloat cellH = LLScreenWidth < 375 ? 100 : 130;
    _imgView = [UIImageView new];
    _imgView.frame = CGRectMake(_selecteBtn.right, 10, 90, 90);
    _imgView.layer.cornerRadius = 5;
    _imgView.contentMode = UIViewContentModeScaleAspectFill;
    _imgView.clipsToBounds = YES;
    _imgView.backgroundColor = UIColor.darkGrayColor;
    [bgView addSubview:_imgView];

    CGFloat marigin = 10;
    _titleLbl = [LLInitBaseViewTool labelWithText:nil textColor:LLColorRGB(0) font:13 textAlignment:NSTextAlignmentLeft];
    _titleLbl.frame = CGRectMake(_imgView.right+marigin, _imgView.top, bgView.width-marigin*2-_imgView.right, 40);
    _titleLbl.numberOfLines = 2;
    [bgView addSubview:_titleLbl];
    
    _priceLbl = [LLInitBaseViewTool labelWithText:nil textColor:nil font:0 textAlignment:NSTextAlignmentLeft];
    _priceLbl.frame = CGRectMake(_titleLbl.left, _imgView.bottom-20, _titleLbl.width-81, 20);
    [bgView addSubview:_priceLbl];

    UIView *doQuantityView = [[UIView alloc] initWithFrame:CGRectMake(bgView.width-81-10, _imgView.bottom-21, 81, 21)];
    doQuantityView.layer.borderColor = LLColorRGB(160).CGColor;
    doQuantityView.layer.borderWidth = 1;
    doQuantityView.layer.cornerRadius = 3;
    doQuantityView.clipsToBounds = YES;
    [bgView addSubview:doQuantityView];

    _reduceBtn = [LLInitBaseViewTool buttonWithTitle:@"－" image:nil titleColor:LLColorRGB(160) font:0];
    _reduceBtn.frame = CGRectMake(0, 0, doQuantityView.height, doQuantityView.height);
    _reduceBtn.tag = 0;
    _reduceBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    [doQuantityView addSubview:_reduceBtn];
    [_reduceBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    CALayer *verticalLine1 = CALayer.layer;
    verticalLine1.frame = CGRectMake(_reduceBtn.right, 0, 1, doQuantityView.height);
    verticalLine1.backgroundColor = LLColorRGB(160).CGColor;
    [doQuantityView.layer addSublayer:verticalLine1];

//    _countLbl = [LLInitBaseViewTool labelWithText:@"1" textColor:LLColorRGB(0) font:20 textAlignment:NSTextAlignmentCenter];
//    _countLbl.frame = CGRectMake(verticalLine1.right, 0, doQuantityView.width-doQuantityView.height*2-2, doQuantityView.height);
//    [doQuantityView addSubview:_countLbl];
    _countF = [[UITextField alloc] initWithFrame:CGRectMake(verticalLine1.right, 0, doQuantityView.width-doQuantityView.height*2-2, doQuantityView.height)];
    _countF.textColor = LLColorRGB(160);
    _countF.font = LLRegularFont(13);
    _countF.textAlignment = NSTextAlignmentCenter;
    [doQuantityView addSubview:_countF];
    
    CALayer *verticalLine2 = CALayer.layer;
    verticalLine2.frame = CGRectMake(_countF.right, 0, 1, doQuantityView.height);
    verticalLine2.backgroundColor = LLColorRGB(160).CGColor;
    [doQuantityView.layer addSublayer:verticalLine2];

    _addBtn = [LLInitBaseViewTool buttonWithTitle:@"＋" image:nil titleColor:LLColorRGB(160) font:0];
    _addBtn.frame = CGRectMake(verticalLine2.right, 0, doQuantityView.height, doQuantityView.height);
    _addBtn.tag = 1;
    _addBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    [doQuantityView addSubview:_addBtn];
    [_addBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)setModel:(LLShoppingCartModel *)model {
    _model = model;
    [_imgView sd_setImageWithURL:LLImageWithUrl(model.imgUrl) placeholderImage:[UIImage imageNamed:@""] options:SDWebImageRefreshCached];
    _titleLbl.text = model.title;
    _priceLbl.attributedText = model.attrPrice;
    _countF.text = model.countStr;
    _selecteBtn.selected = model.isSelected;
}

- (void)selecteBtnClick:(LLButton *)btn {
    if ([self.delegate respondsToSelector:@selector(cellDidClickSelect:)]) {
        [self.delegate cellDidClickSelect:self];
    }
}

- (void)btnClick:(LLButton *)btn {
    
}

@end
